package com.lys.mixin.Item;

import com.lys.enchantment.ModEnchantments;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Random;

@Mixin(BowItem.class)
public abstract class BowItemMixin {

    /**
     * 爆炸和多重箭矢的实现
     */
    @Inject(method = "onStoppedUsing",at = @At(value = "INVOKE",target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"), locals = LocalCapture.CAPTURE_FAILHARD)
    protected void injectOnStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks, CallbackInfo ci, PlayerEntity playerEntity, boolean bl, ItemStack itemStack, int i, float f, boolean bl2, ArrowItem arrowItem, PersistentProjectileEntity persistentProjectileEntity){
        int b;
        int m;
        if (( b = EnchantmentHelper.getLevel(ModEnchantments.EXPLOSIVE_ARROW, stack)) > 0) {
            NbtCompound nbt = new NbtCompound();
            nbt.putInt("explosiveLevel", b);
            if (new Random().nextInt(1, 10) * b > 8) {
                nbt.putBoolean("isExplode", true);
            }
            persistentProjectileEntity.readCustomDataFromNbt(nbt);
        }
        if ((m = EnchantmentHelper.getLevel(ModEnchantments.MULTI_ARROW,stack))>0){
            for (int arrowNum = 0; arrowNum < m; arrowNum++) {
                PersistentProjectileEntity arrow = arrowItem.createArrow(world, itemStack, playerEntity);
                arrow.setVelocity(playerEntity, playerEntity.getPitch(), playerEntity.getYaw(), 0.0f, f * 3.0f, 10.0f);
                if (f == 1.0f) {
                    arrow.setCritical(true);
                }
                int j,k;
                if (( j = EnchantmentHelper.getLevel(Enchantments.POWER, stack)) > 0) {
                    arrow.setDamage(arrow.getDamage() + (double)j * 0.5 + 0.5);
                }
                if (( k = EnchantmentHelper.getLevel(Enchantments.PUNCH, stack)) > 0) {
                    arrow.setPunch(k);
                }
                if (EnchantmentHelper.getLevel(Enchantments.FLAME, stack) > 0) {
                    arrow.setOnFireFor(100);
                }
                //由附魔生成的箭矢不可被拾取
                arrow.pickupType = PersistentProjectileEntity.PickupPermission.DISALLOWED;
                world.spawnEntity(arrow);
            }
        }
    }

    /**
     * 快速拉弓的实现
     */
    @ModifyVariable(method = "onStoppedUsing(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;I)V",at = @At("HEAD"), ordinal = 0, argsOnly = true)
    private int injectOnStoppedUsing(int remainingUseTicks,ItemStack stack, World world, LivingEntity user){
        int level = EnchantmentHelper.getLevel(ModEnchantments.QUICK_PULLING, stack);
        return (int) (remainingUseTicks*(1-level*0.0001F));
    }

}
